#!/bin/bash

dnf install -y wget yum-utils device-mapper-persistent-data lvm2

#设定软件源为阿里镜像中心
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^# baseurl=https://repo.almalinux.org|baseurl=https://mirrors.aliyun.com|g' \
    -i.bak \
    /etc/yum.repos.d/almalinux*.repo
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/dnf.repos.d/docker-ce.repo
dnf clean all
dnf makecache

#关闭防火墙
systemctl stop firewalld.service

#禁用防火墙
systemctl disable firewalld.service

# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

# 关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

# 在master添加hosts
echo -e 192.168.31.220     al31220  >> /etc/hosts
echo -e 192.168.31.221     al31221  >> /etc/hosts
echo -e 192.168.31.222     al31222  >> /etc/hosts
echo -e 192.168.31.223     al31223  >> /etc/hosts


# 将桥接的IPv4流量传递到iptables的链
echo -e  net.bridge.bridge-nf-call-ip6tables = 1 > /etc/sysctl.d/k8s.conf
echo -e  net.bridge.bridge-nf-call-iptables = 1 >> /etc/sysctl.d/k8s.conf

# 生效
sysctl --system

echo vm.max_map_count=262144 >>/etc/sysctl.conf


cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
EOF
setenforce 0

dnf  remove -y kubeadm kubectl kubelet kubernetes-cni cri-tools socat  git docker  docker-ce docker-ce-cli containerd.io docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
dnf install -y  bc initscripts telnet net-tools bash-completion git vim expect rsync curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc  gcc-c++  perl-ExtUtils-MakeMaker  automake autoconf libtool make dnf-utils device-mapper-persistent-data lvm2 docker-ce kubelet kubeadm kubectl bind-utils
systemctl enable kubelet && systemctl start kubelet


#配置docker阿里镜像源加速
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://3mzam45g.mirror.aliyuncs.com",
        "https://dockerhub.azk8s.cn"

    ],
    "hosts": [
        "tcp://0.0.0.0:2375",
        "unix:///var/run/docker.sock"
    ],
    "exec-opts":[
        "native.cgroupdriver=systemd"
    ]
}
EOF

mkdir -p /etc/systemd/system/docker.service.d
echo [Service] >/etc/systemd/system/docker.service.d/docker.conf
echo ExecStart= >>/etc/systemd/system/docker.service.d/docker.conf
echo ExecStart=/usr/bin/dockerd >>/etc/systemd/system/docker.service.d/docker.conf


#刷新配置
systemctl daemon-reload
systemctl enable docker
systemctl restart docker
systemctl enable kubelet
systemctl start kubelet

#将jack用户添加至docker守护者进程
gpasswd -a zzhao docker

